package com.gmall.search.service;

import com.gmall.client.feign.ProductFeignClient;
import com.gmall.client.feign.TrademarkFeignClient;
import com.gmall.common.result.Result;
import com.gmall.model.product.BaseTrademark;
import com.gmall.model.product.SkuInfo;
import com.gmall.model.product.dto.Category123IdNameDTO;
import com.gmall.model.search.Goods;
import com.gmall.rabbitmq.config.Sku_Queue_Config;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

@Component
public class SkuOnCancelSaleService {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    ProductFeignClient productFeignClient;
    
    @Autowired
    TrademarkFeignClient trademarkFeignClient;

    @RabbitListener(queues = Sku_Queue_Config.QUEUE_GOODS_UPPER)
    public void onSale(Channel channel, Message message,Long skuId){

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            Result<SkuInfo> resultSkuInfo = productFeignClient.getSkuInfoById(skuId);
            if (resultSkuInfo.isOk()) {
                SkuInfo skuInfo = resultSkuInfo.getData();

                Goods goods = new Goods();
                goods.setId(skuInfo.getId());

                Result<Category123IdNameDTO> category123IdNameDTOResult
                        = productFeignClient.getCategory123IdNameDTO(skuInfo.getCategory3Id());
                if (category123IdNameDTOResult.isOk()) {
                    Category123IdNameDTO category123IdNameDTO = category123IdNameDTOResult.getData();

                    if (category123IdNameDTO != null) {
                        goods.setCategory1Id(category123IdNameDTO.getCategory1Id());//2, 30, 192
                        goods.setCategory2Id(category123IdNameDTO.getCategory2Id());
                        goods.setCategory3Id(category123IdNameDTO.getCategory3Id());
                        goods.setCategory1Name(category123IdNameDTO.getCategory1Name());
                        goods.setCategory2Name(category123IdNameDTO.getCategory2Name());
                        goods.setCategory3Name(category123IdNameDTO.getCategory3Name());
                    }
                }

                goods.setTitle(skuInfo.getSkuName() + " " + skuInfo.getSkuDesc());
                goods.setTmId(skuInfo.getTmId());
                goods.setTmName(skuInfo.getTmName());
                Result<BaseTrademark> result = trademarkFeignClient.getBaseTrademark(skuInfo.getTmId());
                if (result.isOk()) {
                    goods.setTmLogoUrl(result.getData().getLogoUrl());
                }
                goods.setPrice(skuInfo.getPrice().doubleValue());
                goods.setCreateTime(skuInfo.getCreateTime());
                goods.setUpdateTime(skuInfo.getUpdateTime());

                //4、将返回的数据同步到ES
                System.out.println("将返回的数据同步到ES：" + goods);
                elasticsearchRestTemplate.save(goods);

                channel.basicAck(deliveryTag,false);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = Sku_Queue_Config.QUEUE_GOODS_LOWER)
    public void cancelSale(Channel channel, Message message,Long skuId){

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            elasticsearchRestTemplate.delete(String.valueOf(skuId), Goods.class);
            channel.basicAck(deliveryTag, false);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
